package org.leo.funny.io;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

// 该框架的功能：用文件或数据库保存程序的输出信息
public class WriterTemplate {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    private final DataSource dataSource;
    private Connection conn;
    private final File dest;
    private final Charset charset;
    private RandomAccessFile raf;

    public WriterTemplate(DataSource dataSource) throws SQLException {
        this.dataSource = dataSource;
        this.dest = null;
        this.charset = null;
        if (Objects.nonNull(this.dataSource)) {
            logger.debug("==========获取数据库连接==========");
            this.conn = dataSource.getConnection();
        }
    }

    public WriterTemplate(File dest, Charset charset) throws FileNotFoundException {
        this.dest = dest;
        this.charset = charset;
        this.dataSource = null;
        this.raf = new RandomAccessFile(this.dest, "rw");
    }

    public void write(String message) throws IOException, SQLException {
        if (Objects.nonNull(this.conn)) {
            // 查询当前数据库的funny_message表是否存在
            ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), null,
                    "funny_message", null);
            //  如果funny_message表不存在
            if (!rs.next()) {
                logger.debug("~~~~~~创建funny_message表~~~~~~");
                conn.createStatement().execute("create table funny_message " +
                        "(id int primary key auto_increment, message_text text)");
                rs.close();
            }
            logger.debug("~~~~~~输出到数据表~~~~~~");
            // 插入要输出的字符串
            conn.createStatement().executeUpdate("insert into " +
                    "funny_message values (null, '" + message + "')");
        }
        else {
            logger.debug("~~~~~~输出到文件~~~~~~");
            // System.out.println("~~~~~~输出到文件~~~~~~");
            // 输出到文件
            raf.seek(this.dest.length());
            raf.write((message + "\n").getBytes(this.charset));
        }
    }
    // 关闭资源
    public void close() throws SQLException, IOException {
        if (this.conn != null) {
            this.conn.close();
        }
        if (this.raf != null) {
            this.raf.close();
        }
    }
}
